<?php
/* @var $this yii\web\View */
use backend\models\GoodsCategory;
use yii\bootstrap\ActiveForm;
use yii\bootstrap\Html;
use yii\web\JqueryAsset;

$this->title = "分类添加/修改";

$this->params['breadcrumbs'][] = ['label'=>'分类列表','url'=>['goods-category/list']];

$this->params['breadcrumbs'][]=$this->title;

$models = GoodsCategory::getGoodsCategorys();

//注册js文件
$this->registerJsFile('@web/zTree/js/jquery.ztree.core.js',['depends'=>JqueryAsset::className()]);
//注册样式文件
$this->registerCssFile('@web/zTree/css/zTreeStyle/zTreeStyle.css');
//注册js代码
$jsfile = <<<DOG

    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
        var setting = {
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "parent_id",
                    rootPId: 0
                }
            },
            callback:{
                onClick: function(event, treeId, treeNode){
                    $("#goodscategory-parent_id").val(treeNode.id);
                    //console.dir(treeNode.id);
                }
            },
        };
    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
        var zNodes = {$models};
    //好像是获得当前的树对象
        zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
   
    //设定是否展开树节点
        zTreeObj.expandAll(false);
    //编辑室自动选中当前分类的父节点  parent_id  id
    //当前节点的parent_id ={$model->parent_id};
    //当前节点的父级节点的id是当前节点的parent_id
    //通过查找的到当前对象 的父级对象
    if('$model->parent_id'){
        var node = zTreeObj.getNodeByParam("id",'{$model->parent_id}',null);
         //把找到的对象设置为选中状态
         zTreeObj.selectNode(node);
    }


    /*
    function 举例
    1. 查找 id = 1 的节点数据
    var treeObj = $.fn.zTree.getZTreeObj("tree");
    var node = treeObj.getNodeByParam("id", 1, null);
   
    */
DOG;


$this->registerJs($jsfile);

$form = ActiveForm::begin();
echo $form->field($model,'name');
echo $form->field($model,'parent_id')->hiddenInput();
echo '<div>
    <ul id="treeDemo" class="ztree"></ul>
</div>';

echo $form->field($model,'intro')->textarea();
echo Html::submitButton('提交',['class'=>'btn btn-success']);
ActiveForm::end();



?>

